<?php

namespace App\Admin\Controllers\Stock;

use App\Admin\Actions\Apply;
use App\Models\StockApply;
use App\Http\Controllers\Controller;
use App\Models\StockDepot;
use App\Models\StockUnit;
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use function foo\func;

class StockApplyController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('订货单'))
            ->description(trans('列表'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.detail'))
            ->description(trans('订货单'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.edit'))
            ->description(trans('订货单'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.create'))
            ->description(trans('订货单'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new StockApply);
        if (Admin::user()->depot_id){
            $grid->model()->where('depot_id',Admin::user()->depot_id);
        }
        $userList = Administrator::all()->pluck('name', 'id')->toArray();
        $depotList = StockDepot::all()->pluck('name', 'id')->toArray();
        $grid->model()->orderByDesc('updated_at');
        $grid->id('ID');
        $grid->sn('申请单编号');
//        $grid->name('产品名称');
//        $grid->spec('规格型号');
//        $grid->num('数量');
        $grid->depot_id('仓库')->using($depotList);
        $grid->apply_date('需要时间');
        $grid->creator_id('申请人')->using($userList);
        $grid->examine_status('审核状态')->using([0=>'未审核', 1=>'已审核']);
        $grid->examine_at('审核时间');
        $grid->examine_user('审核人')->using($userList);

        $grid->created_at(trans('admin.created_at'));
        $grid->updated_at(trans('admin.updated_at'));

        $grid->actions(function ($actions) {
            $actions->add(new Apply());
        });

        $grid->filter(function($filter){
            $filter->disableIdFilter();// 去掉默认的id过滤器
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(StockApply::findOrFail($id));

        $userList = Administrator::all()->pluck('name', 'id')->toArray();
        $depotList = StockDepot::all()->pluck('name', 'id')->toArray();
        $show->id('ID');
        $show->sn('申请单编号');
//        $show->name('产品名称');
//        $show->spec('规格型号');
//        $show->num('数量');
        $show->depot_id('仓库')->using($depotList);
        $show->apply_date('需要时间');
        $show->creator_id('申请人')->using($userList);
        $show->examine_status('审核状态')->using([0=>'未审核', 1=>'已审核']);
        $show->examine_at('审核时间');
        $show->examine_user('审核人')->using($userList);
        $show->created_at(trans('admin.created_at'));
        $show->updated_at(trans('admin.updated_at'));
        $show->products('产品', function (Grid $products) {
            $unitList = StockUnit::all()->pluck('name', 'id')->toArray();
            $products->name('产品名称');
            $products->spec('规格型号');
            $products->num('数量');
            $products->unit_id('单位')->using($unitList);
            $products->desc('备注');

            $products->disableActions();
            $products->disableCreateButton();
            $products->disablePagination();
            $products->disableTools();
            $products->disableExport();
            $products->disableBatchActions();
            $products->disableRowSelector();
        });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new StockApply);
        $unitList = StockUnit::all()->pluck('name', 'id')->toArray();
        $depotList = StockDepot::all()->pluck('name', 'id')->toArray();
//        $form->display('ID');
//        $form->text('name', '产品名称');
//        $form->text('spec', '规格型号');
//        $form->text('num', '数量');

        $form->text('sn', '编号')->default('AL'.date("YmdHis").rand(1000, 9999))->readonly();
        $form->date('apply_date', '需要时间')->rules('required');
        $form->select('depot_id', '仓库')->options($depotList)->default(Admin::user()->depot_id)->readOnly();
        $form->hidden('creator_id', '申请人')->default(Admin::user()->id);
//        $form->text('examine_status', '审核状态');
//        $form->text('examine_at', '审核时间');
//        $form->text('examine_user', '审核人');
//        $form->display(trans('admin.created_at'));
//        $form->display(trans('admin.updated_at'));
        $form->hasMany('products', '产品', function ($form) use ($unitList) {
            $form->text('name','名称')->rules('min:2|max:40');
            $form->text('spec','规格');

            $form->decimal('num', '数量')->rules('numeric');
            $form->select('unit_id', '单位')->options($unitList);
            $form->text('desc', '备注');
        });


        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        return $form;
    }
}
